﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLThuChi.Models;
using QLThuChi.Repositories;

namespace QLThuChi.Reports
{
    public partial class FrmPrintPhieuChiVLXD : Form
    {
        private List<PhieuChi_VLXD> _lstPhieuChi;
        private string _congtrinh;

        public FrmPrintPhieuChiVLXD(List<PhieuChi_VLXD> lstItem, string congtrinh)
        {
            InitializeComponent();
            _lstPhieuChi = lstItem;
            _congtrinh = congtrinh;
        }

        private void FrmPrintPhieuChiVLXD_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        public void LoadData()
        {
            var dt = new DataTable();
            dt.Columns.Add("STT");
            dt.Columns.Add("GiaiDoan");
            dt.Columns.Add("NguoiLapPhieu");
            dt.Columns.Add("NgayLapPhieu");
            dt.Columns.Add("VLXD");
            dt.Columns.Add("SoLuong");
            dt.Columns.Add("DonGia");

            long tongchiphi = 0;
            for (int i = 0; i < _lstPhieuChi.Count; i++)
            {
                dt.Rows.Add((i + 1).ToString(), _lstPhieuChi[i].CongTrinh_GiaiDoan.GiaiDoan.TenGiaiDoan, _lstPhieuChi[i].NguoiDung.TenNguoiDung, String.Format("{0:dd/MM/yyyy}", _lstPhieuChi[i].NgayLapPhieu.Value), "", "", "");

                var lstVLXD = ChiTietPhieuRepository.SelectByMaPhieu(_lstPhieuChi[i].MaPhieu);
                long sum = 0;

                for (int j = 0; j < lstVLXD.Count; j++)
                {
                    dt.Rows.Add("", "", "", "", lstVLXD[j].VLXD.TenVLXD, lstVLXD[j].SoLuong, String.Format("{0:#,##0;Nothing}", lstVLXD[j].DonGia));
                    sum += (lstVLXD[j].SoLuong.Value * lstVLXD[j].DonGia.Value);
                }

                tongchiphi += sum;
                dt.Rows.Add("", "", "", "", "THÀNH TIỀN", "", String.Format("{0:#,##0;Nothing}", sum));
            }

            rptThongKePhieuChiVLXD rpt = new rptThongKePhieuChiVLXD();
            rpt.DataDefinition.FormulaFields["TenCongTrinh"].Text = "'" + _congtrinh + "'";
            rpt.DataDefinition.FormulaFields["NgayThangNam"].Text = "'Ngày " + DateTime.Now.Day.ToString() + " tháng " + DateTime.Now.Month.ToString() + " năm " + DateTime.Now.Year.ToString() + "'";
            rpt.DataDefinition.FormulaFields["TongCong"].Text = "'" + String.Format("{0:#,##0;Nothing}", tongchiphi) +"'";
            rpt.SetDataSource(dt);
            crystalReportViewer1.ReportSource = rpt;
        }
    }
}
